/**
 * 道具管理类
 * 
 * 1.隐身
2.冰冻
3.剪刀
4.啤酒
5.变色
6.死亡荷叶
7.生成荷叶
 * 
 * 
 * 
 * 
 * 
 * 
 */
class AIPropsMgr {
	private static instance: AIPropsMgr;
	public static getInstance(): AIPropsMgr {
		if (this.instance == null) {
			this.instance = new AIPropsMgr();
		}
		return this.instance;
	}


	//用缓存池子的方式来创建道具，独立模块，跟其他模块没有冲突


	public propsPool = [];

	//用于
	public dieLeafPool = [];
	private roomProps = [];

	public totalProps = 0;

	private c = serverConfig.config;

	/**玩家管理 */
	private userMgr = AIUserMgr.getInstance();




	// 1.创建道具
	// 2.获取道具
	// 3.返还道具
	// 4.获取道具数目
	// gy



	//将指定房间的道具进行初始化
	public roomPorpsInit() {
		// //取出指定的房间
		var garbageProp = 0;
		//归还所有的道具
		for (var t = 0; t < this.roomProps.length; t++) {

			//清空所有存在切不使用的道具
			if (this.roomProps[t].isUsed) {
				this.recoverProp(this.roomProps[t]);
				garbageProp++;
			}

		}
		//console.log("roommgr 回收道具" + t);
		//console.log("roommgr 回收过道具" + garbageProp);
		//清空整个缓存池子
		this.roomProps = [];

		//创建新的道具
		this.generateProps();

		//	console.log("道具重生后" + this.roomProps.length);

		return this.roomProps;
	}

	/**
	 * 创建所有的随机道具
	 */
	private generateProps() {
		if (DataProl.isXinShou) {
			return;
		}
		else {
			for (var i = 0; i < this.c.propMaxnum; i++) {
				this.generateProp();
			}
		}
	}
	public getRoomProps() {
		return this.roomProps;
	}
	//判断所有道具和线的距离
	/**
	 * 判断道具和线的距离，如果有道具消失了，则重新生成道具，然后通知给服务器
	 * @param {*} lines 
	 * @param {*} props 
	 */
	public newUserPorpDied(roomUser, props) {
		var usrstart;
		var usrend;
		var userPoint;

		var isRemoveProp = false;
        /**
         * 记录重生的道具的数量
         */
		var rebornPorpNumbser = 0;
		for (let m = (props.length - 1); m >= 0; m--) {
			isRemoveProp = false;
			for (let n = 0; n < roomUser.length; n++) {

				if (roomUser[n] == null)
					continue;

				if (roomUser[n].isDeath)
					continue;

				if (isRemoveProp) {

					//console.log("该道具已经被上个线消除过");
					continue;
				}
				userPoint = { x: props[m].x, y: props[m].y };
				if (this.isCrash(50, userPoint, roomUser[n])) {
					//console.log("碰到道具");
					if (roomUser[n].propStates[3].getPropType == 4 && DataProl.isXinShou) {//新手引导喝酒状态中吃不到道具
						return;
					}
					this.changeSinglePlayerState(props[m], roomUser[n]);
					this.recoverProp(this.roomProps[m]);
					if (props[m].type != 7 && !DataProl.isXinShou) {
						rebornPorpNumbser++;
					}
					if (this.findIndex(props, props[m].id) > -1)
						props.splice(this.findIndex(props, props[m].id), 1);
					isRemoveProp = true;
				}
			}
		}
		return rebornPorpNumbser;
	}
	/**获取随机颜色 */
	public getRandomColor(playColor: number) {
		let randomColor = playColor;
		while (playColor == randomColor) {
			randomColor = Math.floor(Math.random() * this.c.maxFrigtype);
		}
		return randomColor;
	}
	/**更改单个玩家的状态 */
	public changeSinglePlayerState(prop, Player) {
		if (prop.type == 2)/**冰冻 */ {
			Player.speed = 0;
		}
		if (prop.type == 9)/**异色心碎 */ {
			Player.speed = 0;
		}
		if (prop.type == 8)/**眩晕 */ {
			Player.speed = 0;
		}
		if (prop.type == 5) {
			//Player.displayEffectOfProps(prop.type) ;
			var frogtype = Player.frogType;
			if (Player.ishadLine) {
				frogtype = Player.frogType;
				return;
			} else {
				do {
					frogtype = this.getRandomColor(Player.frogType);
				}
				while (frogtype == Player.frogType);
			}
			Player.frogType = frogtype;
			// do {
			// 	//Player.displayEffectOfProps(prop.type) ;
			// 	var frogtype = Player.frogType;
			// 	Player.frogType = Math.floor(Math.random() * this.c.maxFrigtype);
			// } while (frogtype == Player.frogType);
		}
		if (prop.type == 7 || prop.type == 6) {
			if (!Player.ishadLine) {
				if (prop.isUsed) {
					Player.grade += prop.grade;
					Player.lineLength = this.GetAttributesByGrade(Player.grade).frogLineLength;
					Player.level = this.GetAttributesByGrade(Player.grade).level;
				}
			}
			else {
				if (webNetworkMananger.getInstance().gameSceneRec != null) {
					if (webNetworkMananger.getInstance().gameSceneRec.m_createAllFrogs != null) {
						var frog = webNetworkMananger.getInstance().gameSceneRec.m_createAllFrogs.getLigatureUserId(Player.userId)
						if (frog != null) {
							Player.grade += Math.round(prop.grade / 2);
							frog.grade += Math.round(prop.grade / 2);
							Player.level = this.GetAttributesByGrade(Player.grade).level
							frog.level = this.GetAttributesByGrade(frog.grade).level
							Player.lineLength = this.GetAttributesByGrade(Player.grade).frogLineLength;
							frog.lineLength = this.GetAttributesByGrade(frog.grade).frogLineLength;
						}
					}
				}
			}
		}
		//无论碰到了任何道具，则更改其状态，同时也不改变其状态
		//if (prop.type != 7 && prop.type != 6) {
		if (prop.type != 7) {
			Player.propStates[prop.type - 1].getPropType = prop.type;
		}
		if (prop.type != 7 && prop.type != 6) {
			Player.propStates[prop.type - 1].PropStartTime = Date.now();
			//Player.propStates.lastProp=lastPropstates.type
		}
		if (DataProl.isXinShou && prop.type < 7) {
			webNetworkMananger.getInstance().gameSceneRec.displayer_prop_message(this.changeGuideType(prop.type) - 1);//xinshou中的单个道具面板显示
			return;
		}
	}

	//根据策划要求在新手引导显示相应的道具
	public changeGuideType(type) {
		let tmp = 0;
		switch (type) {
			case 1:
				tmp = 4;
				break;
			case 2:
				tmp = 3;
				break;
			case 3:
				tmp = 5;
				break;
			case 4:
				tmp = 6;
				break;
			case 5:
				tmp = 2;
				break;
			case 6:
				tmp = 1;
				break;
			default:
				tmp = 0;
				break;
		}
		return tmp;
	};
	/**通过分数获取(舌长、舌宽、等级)属性
	 * @param grade 分数
	 */
	public GetAttributesByGrade(grade) {
		/**舌宽 */
		let tongueWideData: number = 0;
		/**线长 */
		let lineLength: number = 0;
		/**等级 */
		let rank: number = 0;
		if (parseInt(grade) >= 15000) {
			tongueWideData = 15;
			lineLength = 600;
			rank = 41;
		} else if (parseInt(grade) >= 9000) {
			tongueWideData = 13;
			lineLength = 500;
			rank = 36;
		} else if (parseInt(grade) >= 7800) {
			tongueWideData = 13;
			lineLength = 460;
			rank = 35;
		} else if (parseInt(grade) >= 6600) {
			tongueWideData = 12;
			lineLength = 420;
			rank = 34;
		} else if (parseInt(grade) >= 5600) {
			tongueWideData = 12;
			lineLength = 380;
			rank = 33;
		} else if (parseInt(grade) >= 4800) {
			tongueWideData = 11;
			lineLength = 350;
			rank = 32;
		} else if (parseInt(grade) >= 4000) {
			tongueWideData = 11;
			lineLength = 320;
			rank = 31;
		} else if (parseInt(grade) >= 3200) {
			tongueWideData = 10;
			lineLength = 280;
			rank = 26;
		} else if (parseInt(grade) >= 2600) {
			tongueWideData = 10;
			lineLength = 260;
			rank = 25;
		} else if (parseInt(grade) >= 2100) {
			tongueWideData = 9;
			lineLength = 240;
			rank = 24;
		} else if (parseInt(grade) >= 1700) {
			tongueWideData = 9;
			lineLength = 220;
			rank = 23;
		} else if (parseInt(grade) >= 1300) {
			tongueWideData = 8;
			lineLength = 200;
			rank = 22;
		} else if (parseInt(grade) >= 1000) {
			tongueWideData = 8;
			lineLength = 180;
			rank = 21;
		} else if (parseInt(grade) >= 700) {
			tongueWideData = 7;
			lineLength = 150;
			rank = 16;
		} else if (parseInt(grade) >= 550) {
			tongueWideData = 7;
			lineLength = 140;
			rank = 15;
		} else if (parseInt(grade) >= 400) {
			tongueWideData = 6;
			lineLength = 130;
			rank = 14;
		} else if (parseInt(grade) >= 250) {
			tongueWideData = 6;
			lineLength = 120;
			rank = 13;
		} else if (parseInt(grade) >= 100) {
			tongueWideData = 5;
			lineLength = 110;
			rank = 12;
		} else {
			tongueWideData = 5;
			lineLength = 100;
			rank = 11;
		}
		return {
			tongueWide: tongueWideData * frogConfig.Tongue_width_coefficient,
			frogLineLength: lineLength * frogConfig.Long_tongue_coefficient,
			level: rank,
		}
	}

	public isCrash(maxlength, p1, p2) {
		if (Util.getInstance().isNull(maxlength) || Util.getInstance().isNull(p1) || Util.getInstance().isNull(p2)) {
			console.log("there is null");
			return false;
		}

		let lenght = this.getDistance(p1, p2);

		return maxlength > lenght ? true : false;
	}
	//判断所有道具和线的距离
	/**
	 * 判断道具和线的距离，如果有道具消失了，则重新生成道具，然后通知给服务器
	 * @param {*} lines 
	 * @param {*} props 
	 */
	public userPorpDied(lines, props) {
		var usrstart;
		var usrend;
		var userPoint;

		var isRemoveProp = false;
		/**
		 * 记录重生的道具的数量
		 */
		var rebornPorpNumbser = 0;
		for (var m = (props.length - 1); m >= 0; m--) {
			isRemoveProp = false;
			for (var n = 0; n < lines.length; n++) {
				if (lines[n] == null)
					continue;
				if (lines[n].userOneId < 0 || lines[n].userTwoId < 0)
					continue;
				usrstart = this.userMgr.get(lines[n].userOneId);
				if (usrstart == null) {
					console.log("usrstart==null");
					continue;
				}
				usrend = this.userMgr.get(lines[n].userTwoId);
				if (usrend == null) {
					console.log("usrend==null");
					continue;
				}
				if (isRemoveProp) {
					//console.log("该道具已经被上个线消除过");
					continue;
				}
				userPoint = { x: props[m].x, y: props[m].y };
				if (this.checkLineIsOnALine(userPoint, usrend, usrstart)) {
					if (!usrend.isAI) {
						SoundManager.getInstance().playEffectSound("shutdown_mp3");
					}
					if (!usrstart.isAI) {
						SoundManager.getInstance().playEffectSound("shutdown_mp3");
					}
					//console.log("消除道具的位置" + userPoint.x + "," + userPoint.y);
					this.recoverProp(props[m]);
					if (props[m].type != 7) {
						rebornPorpNumbser++;
					}
					if (this.findIndex(props, props[m].id) > -1)
						props.splice(this.findIndex(props, props[m].id), 1);
					isRemoveProp = true;
				}

			}

		}
		return rebornPorpNumbser;
	}
	/**生成一个道具并添加到对象池 */
	public createOneProp(index) {
		this.propsPool.push({
			isUsed: false,
			id: index,
			x: 0,
			y: 0,
			type: 0,
			tweenx: 0,
			tweeny: 0,
			grade: 0,
			isInRange: true
		});
		this.totalProps++;
	}

	public createdieLeafProp(index) {
		this.dieLeafPool.push({
			isUsed: false,
			id: index + 100,
			x: 0,
			y: 0,
			type: 0,
			tweenx: 0,
			tweeny: 0,
			grade: 0,
			isInRange: true
		});
		this.totalProps++;
	}



	public createExtOneProp() {
		this.totalProps++;
		this.propsPool.push({
			isUsed: false,
			id: this.totalProps,
			x: 0,
			y: 0,
			type: 0,
			tweenx: 0,
			tweeny: 0,
			grade: 0,
			isInRange: true
		});
		return this.propsPool[this.totalProps - 1];

	}



	public createProps() {
		this.totalProps = 0;
		if (this.c == null) {
			console.log('create room fail!!!!config==null');
			return;
		}
		var numbers = this.c.propMaxnum;
		if (DataProl.isXinShou) {
			numbers = 6
		}
		if (numbers <= 0) {
			console.log('create room fail!!!!num+numbers');
			return;
		}
		console.log('创建道具数目' + numbers);
		//遍历所有道具
		for (var i = 0; i < numbers; ++i) {
			this.createOneProp(i);

		}

		for (var i = 0; i < numbers; ++i) {
			this.createdieLeafProp(i);

		}
	};
	/**清空房间内所有道具 */
	public cleanRoomAI() {
		for (var i = 0; i < this.propsPool.length; ++i) {
			this.recoverProp(this.propsPool[i]);
		}

		this.dieLeaf();
		this.roomProps = [];
		AIServer.getInstance().roomInfo.roomProps = [];
	}

	public dieLeaf() {
		for (var i = 0; i < this.dieLeafPool.length; ++i) {
			this.recoverProp(this.dieLeafPool[i]);
		}

	}
	/**新手引导清空房间内所有道具 */
	public cleanRoom_Allprop() {
		for (var i = 0; i < this.propsPool.length; ++i) {
			this.recoverProp(this.propsPool[i]);
		}
		this.dieLeaf();
	}
	public restAllProps() {
		this.cleanRoomAI();
		this.roomProps = [];
		this.totalProps = 0;
	}

	/**
	 * 回收不使用的道具
	 */
	public recoverProp(prop) {
		if (prop != null) {
			prop.isUsed = false;
		}
	};
	/**创建新手引导道具 一组 */
	public NewbieProp(i: any) {

		var position = this.getPropPutPoint(i);
		//获取一个道具
		var prop = this.getOneProp();
		if (prop == null) {
			console.log('道具创建失败d:');
			return;
		}
		prop.x = position.x * DataProl.gameSceneScal + 100;
		prop.y = position.y * DataProl.gameSceneScal + 100;
		prop.type = i + 1;
		if (prop.type == 6) {
			prop.grade = 9000;
		}
		prop.isUsed = true;
		this.roomProps.push(prop);

	}
	/**获取新手引导道具摆放位置
	 * @param num 道具序号
	 */
	public getPropPutPoint(num: number) {
		switch (num) {
			case 0:
				return { x: GlobalData.GameStage.stageWidth / 4, y: GlobalData.GameStage.stageHeight / 3 * 2 };
			case 1:
				return { x: GlobalData.GameStage.stageWidth / 4 * 3, y: GlobalData.GameStage.stageHeight / 3 };
			case 2:
				return { x: GlobalData.GameStage.stageWidth / 2, y: GlobalData.GameStage.stageHeight / 3 * 2 };
			case 3:
				return { x: GlobalData.GameStage.stageWidth / 4 * 3, y: GlobalData.GameStage.stageHeight / 3 * 2 };
			case 4:
				return { x: GlobalData.GameStage.stageWidth / 2, y: GlobalData.GameStage.stageHeight / 3 };
			case 5:
				return { x: GlobalData.GameStage.stageWidth / 4, y: GlobalData.GameStage.stageHeight / 3 };
		}

	}
	/**创建新手引导道具 一个 */
	public NewbieOneProp(type, point) {
		//获取一个道具
		var prop = this.getOneProp();
		if (prop == null) {
			console.log('道具创建失败d:');
			return;
		}
		prop.x = point.x;
		prop.y = point.y;
		prop.type = type;
		if (prop.type == 6) {
			prop.grade = 20;
		}
		prop.isUsed = true;
		this.roomProps.push(prop);
		AIServer.getInstance().roomInfo.roomProps = this.roomProps;
	}
	/**创建一个道具 */
	public generateProp() {
		var position = this.c.foodUniformDisposition ? this.uniformPosition(this.roomProps, serverConfig.config.gameMapBorder) : this.randomPosition(serverConfig.config.gameMapBorder);
		//获取一个道具
		var prop = this.getOneProp();
		if (prop == null) {
			console.log('道具创建失败d:');
			return;
		}
		prop.x = position.x;
		prop.y = position.y;
		//检查一下道具数量
		var nowallprop = this.allPropnum();
		this.roomProps.push(prop);
		//比较一下道具数量
		var lackprop = this.comparenum(nowallprop);
		if (lackprop != null && lackprop != undefined) {
			prop.type = lackprop.type;
			if (prop.type == 6) {
				prop.grade = lackprop.grade;
				return;
			}

			//  console.log("重生的道具的位置" + prop.x + "," + prop.y);
			//this.roomProps.push(prop);
		}
		else { return; }

	}
	/**检查当前道具数量 */
	public comparenum(nowallprop) {
		if (nowallprop[0].num < this.c.propRatio.hide * this.c.propMaxnum) {
			return { type: nowallprop[0].type, grade: nowallprop[0].grade };
		}
		else if (nowallprop[1].num < this.c.propRatio.ice * this.c.propMaxnum) {
			return { type: nowallprop[1].type, grade: nowallprop[1].grade };
		}
		else if (nowallprop[2].num < this.c.propRatio.cut * this.c.propMaxnum) {
			return { type: nowallprop[2].type, grade: nowallprop[2].grade };
		}
		else if (nowallprop[3].num < this.c.propRatio.drink * this.c.propMaxnum) {
			return { type: nowallprop[3].type, grade: nowallprop[3].grade };
		}
		else if (nowallprop[4].num < this.c.propRatio.change * this.c.propMaxnum) {
			return { type: nowallprop[4].type, grade: nowallprop[4].grade };
		}
		else if (nowallprop[5].num < this.c.propRatio.minleaf * this.c.propMaxnum) {
			return { type: nowallprop[5].type, grade: nowallprop[5].grade };
		}
		else if (nowallprop[6].num < this.c.propRatio.lesserleaf * this.c.propMaxnum) {
			return { type: nowallprop[6].type, grade: nowallprop[6].grade };
		}
		else if (nowallprop[7].num < this.c.propRatio.mediumleaf * this.c.propMaxnum) {
			return { type: nowallprop[7].type, grade: nowallprop[7].grade };
		}
		else if (nowallprop[8].num < this.c.propRatio.maxleaf * this.c.propMaxnum) {
			return { type: nowallprop[8].type, grade: nowallprop[8].grade };
		}
	}
	/**计算道具数量 */
	public allPropnum() {
		//所有道具当前数量数组
		let nowallprop = []

		// 将所有道具存如数组依次为隐身，冰，剪刀，喝酒，变色，
		//2分荷叶，5分荷叶，20分荷叶，50分荷叶
		for (var i = 0; i < 9; i++) {
			//type  依次为隐身，冰，剪刀，喝酒，变色，荷叶
			let nowprop =
				{
					type: 0,
					num: 0,
					grade: 0,
				}
			//给道具赋值类型和分数 避免第一次为空
			if (i < 5) {
				nowprop.type = i + 1;
			}
			if (i >= 5) {
				nowprop.type = 6
			}
			nowallprop.push(nowprop);
		}
		nowallprop[5].grade = 2
		nowallprop[6].grade = 5
		nowallprop[7].grade = 20
		nowallprop[8].grade = 50
		//遍历房间列表 记录道具数
		for (var i = 0; i < this.roomProps.length; i++) {
			let prop = this.roomProps[i];
			//给当前道具数量数组赋值
			this.propnum(prop, nowallprop);
		}
		return nowallprop;
	}
	/**给当前道具数量数组赋值 */
	public propnum(prop, nowallprop) {
		switch (prop.type) {
			case 1:
				nowallprop[0].type = 1;
				nowallprop[0].num++;
				break;
			case 2:
				nowallprop[1].type = 2;
				nowallprop[1].num++;
				break;
			case 3:
				nowallprop[2].type = 3;
				nowallprop[2].num++;
				break;
			case 4:
				nowallprop[3].type = 4;
				nowallprop[3].num++;
				break;
			case 5:
				nowallprop[4].type = 5;
				nowallprop[4].num++;
				break;
			case 6:
				if (prop.grade == 2) {
					nowallprop[5].type = 6;
					nowallprop[5].num++;
					nowallprop[1].grade = 2;
				}
				if (prop.grade == 5) {
					nowallprop[6].type = 6;
					nowallprop[6].num++;
					nowallprop[6].grade = 5;
				}
				if (prop.grade == 20) {
					nowallprop[7].type = 6;
					nowallprop[7].num++;
					nowallprop[7].grade = 20;
				}
				if (prop.grade == 50) {
					nowallprop[8].type = 6;
					nowallprop[8].num++;
					nowallprop[8].grade = 50;
				}
				break;
		}
	}
	/**荷叶生成 */
	public createLeaf(x: number, y: number, lineLength: any) {
		//获取荷叶分数和数量数组
		var leafarr = this.cteateLeafNum(lineLength);
		if (leafarr == null) { return; }
		//生成荷叶并给荷叶定位
		for (var i = 0; i < leafarr.length; i++) {
			var prop = this.getOneDieLeaf();
			if (prop == null) {
				console.log('道具创建失败d:');
				return;
			}
			//给荷叶赋位置
			this.LeafPlace(prop, leafarr, i, x, y);
			//给荷叶的tween动画其实位置赋值
			prop.tweenx = x;
			prop.tweeny = y;
			prop.type = 7;
			this.roomProps.push(prop);
		//	console.log("生成荷叶");
		}
	}
	/**荷叶位置 */
	public LeafPlace(prop, leafarr, i, x, y) {
		switch (i) {
			case 0:
				prop.x = x + 100
				prop.y = y
				prop.grade = leafarr[i]
				break;
			case 1:
				prop.x = x - 100
				prop.y = y
				prop.grade = leafarr[i]
				break;
			case 2:
				prop.x = x
				prop.y = y + 100
				prop.grade = leafarr[i]
				break;
			case 3:
				prop.x = x
				prop.y = y - 100
				prop.grade = leafarr[i]
				break;
			case 4:
				prop.x = x + 50
				prop.y = y + 87
				prop.grade = leafarr[i]
				break;
			case 5:
				prop.x = x + 87
				prop.y = y - 50
				prop.grade = leafarr[i]
				break;
			case 6:
				prop.x = x - 50
				prop.y = y - 87
				prop.grade = leafarr[i]
				break;
			case 7:
				prop.x = x - 50
				prop.y = y + 87
				prop.grade = leafarr[i]
				break;
			case 8:
				prop.x = x + 87
				prop.y = y + 50
				prop.grade = leafarr[i]
				break;
			case 9:
				prop.x = x + 50
				prop.y = y - 87
				prop.grade = leafarr[i]
				break;
			case 10:
				prop.x = x - 87
				prop.y = y - 50
				prop.grade = leafarr[i]
				break;
			case 11:
				prop.x = x - 87
				prop.y = y + 50
				prop.grade = leafarr[i]
				break;
		}
		//检测荷叶位置是否在界内
		this.determinePosition(prop);
	}
	/**计算生成荷叶数量与分数 */
	public cteateLeafNum(lineLength: any) {
		//存放分数数组
		var leafgrade = [];
		//将青蛙分数取整 不足50按50计算
		var tens = lineLength / 10 % 10
		if (tens < 5) {
			lineLength += (5 - tens) * 10;
		}
		if (tens > 5) {
			lineLength += (10 - tens) * 10;
		}
		//荷叶总分
		var leafmaxgrade = lineLength * 0.4;

		//通过循环试出min--最小荷叶less--稍小荷叶
		//med--稍大荷叶  max 大荷叶的数量并赋分存入数组
		for (var min = 0; min < 12; min++) {
			for (var less = 0; less < 12; less++) {
				for (var med = 0; med < 12; med++) {
					for (var max = 0; max < 12; max++) {
						if (lineLength * 0.4 <= 600) {
							if (min + less + med + max > 4 && min + less + med + max < 13 && 2 * min + 5 * less + 20 * med + 50 * max == leafmaxgrade) {
								for (var num = 0; num < min; num++) {
									leafgrade.push(2);
								}
								for (var num = 0; num < less; num++) {
									leafgrade.push(5);
								}
								for (var num = 0; num < med; num++) {
									leafgrade.push(20);
								}
								for (var num = 0; num < max; num++) {
									leafgrade.push(50);
								}
								return leafgrade;
							}
						}
						//如果青蛙分数的40%大于600
						//即50分荷叶12个不能满足时，将荷叶分数更改为大于50
						else {
							for (var i = 0; i < 12; i++) {
								leafgrade.push(Math.round(leafmaxgrade / 12));
							}
							return leafgrade;
						}
					}
				}
			}
		}
		return leafgrade;
	}
	/**判断荷叶位置 */
	public determinePosition(prop: any) {
		if (prop.x > this.c.gameWidth - 55) {
			prop.x = this.c.gameWidth - 55;
		}
		if (prop.y > this.c.gameHeight - 65) {
			prop.y = this.c.gameHeight - 65;
		}
		if (prop.x < 50) {
			prop.x = 50;
		}
		if (prop.y < 60) {
			prop.y = 60;
		}
	}


	//获取一个死亡的荷叶
	public getOneDieLeaf() {
		for (var i = 0; i < this.dieLeafPool.length; ++i) {
			if (!this.dieLeafPool[i].isUsed) {
				this.dieLeafPool[i].isUsed = true;
				return this.dieLeafPool[i];
			}
		}
	}


	public getOneProp() {
		//获取当前没有使用的道具
		for (var i = 0; i < this.propsPool.length; ++i) {
			if (!this.propsPool[i].isUsed) {
				this.propsPool[i].isUsed = true;
				return this.propsPool[i];
			}
		}
		//没有未使用的则再创建一个
		// if (this.c.isproppoolext)
		// 	return this.createExtOneProp();
		// else
		// 	return;

	};

	public getTotalProps() {
		return this.totalProps;
	};

	public uniformPosition(points, radius) {
		var bestCandidate, maxDistance = 0;
		var numberOfCandidates = 10;

		if (points.length === 0) {
			return this.randomPosition(radius);
		}

		// Generate the candidates
		for (var ci = 0; ci < numberOfCandidates; ci++) {
			var minDistance = Infinity;
			var candidate: any = this.randomPosition(radius);
			candidate.radius = radius;

			for (var pi = 0; pi < points.length; pi++) {
				var distance = this.getDistance(candidate, points[pi]);
				if (distance < minDistance) {
					minDistance = distance;
				}
			}

			if (minDistance > maxDistance) {
				bestCandidate = candidate;
				maxDistance = minDistance;
			} else {
				return this.randomPosition(radius);
			}
		}

		return bestCandidate;
	};

	private randomInRange(from, to) {
		return Math.floor(Math.random() * (to - from)) + from;
	};

	// generate a random position within the field of play
	private randomPosition(radius) {
		return {
			x: this.randomInRange(radius, gameGlobal.global.gameWidth - radius),
			y: this.randomInRange(radius, gameGlobal.global.gameHeight - radius)
		};
	};

	private getDistance = function (startPoint, endPoint) {
		var x = Math.abs(endPoint.x - startPoint.x);
		var y = Math.abs(endPoint.y - startPoint.y);
		return Math.sqrt(x * x + y * y);
	}

	public checkLineIsOnALine(something, usrend, usrstart) {


		/**点到线的距离 */
		var pointToLineLength = this.getPointToLineDistance(usrend, usrstart, something);
		//  console.log("pointToLineLength="+pointToLineLength);
		return pointToLineLength < 50 ? true : false;
	}

	public findIndex(arr, id) {
		var len = arr.length;

		while (len--) {
			if (arr[len].id === id) {
				return len;
			}
		}

		return -1;
	};


	public getPointToLineDistance = function (p1, p2, p) {
		var ans = 0;
		var a = this.getDistance(p1, p2);
		var b = this.getDistance(p1, p);
		var c = this.getDistance(p2, p);
		if (c + b == a) {//点在线段上
			ans = 0;
			return ans;
		}
		if (a <= 0.00001) {//不是线段，是一个点
			ans = b;
			return ans;
		}
		if (c * c >= a * a + b * b) { //组成直角三角形或钝角三角形，p1为直角或钝角
			ans = b;
			return ans;
		}
		if (b * b >= a * a + c * c) {// 组成直角三角形或钝角三角形，p2为直角或钝角
			ans = c;
			return ans;
		}
		// 组成锐角三角形，则求三角形的高
		var p0 = (a + b + c) / 2;// 半周长
		var s = Math.sqrt(p0 * (p0 - a) * (p0 - b) * (p0 - c));// 海伦公式求面积
		ans = 2 * s / a;// 返回点到线的距离（利用三角形面积公式求高）
		return ans;
	}
}